// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
import MapData from "../map/base/MapData";
import {MapType} from "../map/base/MapType";
@ccclass
export default class NewClass extends cc.Component {
    // LIFE-CYCLE CALLBACKS:
    @property(cc.Label)
    inputFileName:cc.Label = null;//当前选择的文件 名label
    @property(cc.EditBox)
    nameLabel:cc.EditBox = null;//当前文件名
    @property(cc.EditBox)
    mapWidthLabel:cc.EditBox = null;
    @property(cc.EditBox)
    mapHeightLabel:cc.EditBox = null;
    @property(cc.EditBox)
    nodeWidthLabel:cc.EditBox = null;
    @property(cc.EditBox)
    nodeHeightLabel:cc.EditBox = null;
    @property(cc.Label)
    tipsLabel:cc.Label = null;
    bgImg:cc.Texture2D = null;
    
    // onLoad () {}
    private _mapInfo:any = {};
    private _defaultConfig:MapData = null;
    start () {
        this.bgImg = new cc.Texture2D();
        let self = this;
        cc.director.on("onLoadImgOver",function(data){
            self.setMapInfo(data);
        });
    }

    setMapData(mapData:MapData){
        this._defaultConfig = mapData;
    }

    //点击确定
    onOkClick(){
        this._defaultConfig.name = this.nameLabel.string;
        if(this._defaultConfig.name == ""){
            this.tipsLabel.string = "请输入地图名称"
            return;
        }
        this.updateMapConfig();
        this.node.active = false;
        let sceneMap = cc.find("Canvas/SceneMap").getComponent("SceneMap");
        sceneMap.init(this._defaultConfig,this.bgImg);
    }

    //根据地图信息生成路径节点信息
    createRoadDataArr(){
        let roadDataArr = [];
        let mapWidth = this._defaultConfig.mapWidth;
        let mapHeight = this._defaultConfig.mapHeight;
        let nodeWidth = this._defaultConfig.nodeWidth;
        let nodeHeight = this._defaultConfig.nodeHeight;
        
        let col = Math.ceil(mapWidth / nodeWidth);
        let row = Math.ceil(mapHeight / nodeHeight);
        
        let mapType = this._defaultConfig.type;
        
        switch(mapType)
        {
            case MapType.honeycomb:
                
                //this._nodeHeight = (this._nodeWidth / 2) * 1.732;
                col = Math.ceil((mapWidth - nodeWidth / 4) / (nodeWidth / 4 * 6)) * 2;
                row = Math.ceil((mapHeight - nodeHeight / 2) / nodeHeight);
        }
        for(let i =0;i<row;i++){
            roadDataArr[i] = [];
            for(let j = 0;j<col;j++){
                roadDataArr[i][j] = 1;
            }
        }
        return roadDataArr;
    }

    //点击取消
    onCancelClick(){
        this.node.active = false;
    }

    setMapInfo(mapInfo:any):void{
        if(this.node.active == true){        
            this._mapInfo = mapInfo
            this._defaultConfig.bgName = this._mapInfo.name;
            this._defaultConfig.mapWidth = this._mapInfo.width;
            this._defaultConfig.mapHeight = this._mapInfo.height;
            this.updateLabelInfo();
            this.updateMapConfig();
        }
    }
    
    //根據當前輸入參數刷新配置信息
    updateMapConfig(){
        this._defaultConfig.nodeWidth = parseInt(this.nodeWidthLabel.string);
        this._defaultConfig.nodeHeight = parseInt(this.nodeHeightLabel.string);
        this._defaultConfig.name = this.nameLabel.string;
        this.bgImg.initWithElement(this._mapInfo.img);
        this._defaultConfig.roadDataArr = this.createRoadDataArr(); //根据新的地图生成路径点
        this.tipsLabel.string = ""
    }

    updateLabelInfo(){
        this.inputFileName.string = this._mapInfo.name;
        this.nodeWidthLabel.string = this._defaultConfig.nodeWidth+"";
        this.nodeHeightLabel.string = this._defaultConfig.nodeHeight+"";
        this.mapWidthLabel.string = this._defaultConfig.mapWidth+"";
        this.mapHeightLabel.string = this._defaultConfig.mapHeight+"";
    }

    onReadFileClick(){
        document.getElementById("file").click();
    }
    // update (dt) {}
}
